package stark.app.function.ae;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;

import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.UriUtils;
import com.stark.audio.edit.AeEntry;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import stark.app.R;
import stark.app.databinding.ActivityAudioEditDemoBinding;
import stark.common.basic.base.BaseNoModelActivity;
import stark.common.basic.utils.IntentUtil;

/**
 * Author：daizhongbin on 2021/6/30
 * Email：1027154338@qq.com
 * Desc:
 */
public class AudioEditDemoActivity extends BaseNoModelActivity<ActivityAudioEditDemoBinding> {

    /**
     * 音频变调变速请求码
     */
    private static final int REQ_AUDIO_FOR_SP = 1;
    /**
     * 音频格式转换请求码
     */
    private static final int REQ_AUDIO_FOR_FMT_CONVERT = 2;
    /**
     * 音频变声请求码
     */
    private static final int REQ_AUDIO_FOR_VOICE_CHANGE = 3;
    /**
     * 音频裁剪请求码
     */
    private static final int REQ_AUDIO_FOR_CROP = 4;
    /**
     * 音频拼接请求码
     */
    private static final int REQ_AUDIO_FOR_CONCAT = 5;
    /**
     * 音频混音请求码
     */
    private static final int REQ_AUDIO_FOR_MIX = 6;
    /**
     * 音频混音请求码带返回结果
     */
    private static final int REQ_AUDIO_FOR_MIX_FOR_RET = 7;

    /**
     * 混音操作请求码
     */
    private static final int REQ_EDIT_AUDIO_FOR_MIX = 100;

    @Override
    protected int onCreate() {
        return R.layout.activity_audio_edit_demo;
    }

    @Override
    protected void initView() {
        mDataBinding.btnSpeedPitch.setOnClickListener(v -> {
            selectAudio(REQ_AUDIO_FOR_SP);
        });
        mDataBinding.btnVoiceChange.setOnClickListener(v -> {
            selectAudio(REQ_AUDIO_FOR_VOICE_CHANGE);
        });
        mDataBinding.btnFmtConvert.setOnClickListener(v -> {
            selectAudio(REQ_AUDIO_FOR_FMT_CONVERT);
        });
        mDataBinding.btnAudioCrop.setOnClickListener(v -> {
            selectAudio(REQ_AUDIO_FOR_CROP);
        });
        mDataBinding.btnAudioConcat.setOnClickListener(v -> {
            selectAudio(REQ_AUDIO_FOR_CONCAT);
        });
        mDataBinding.btnAudioMix.setOnClickListener(v -> {
            selectAudio(REQ_AUDIO_FOR_MIX);
        });
        mDataBinding.btnAudioMixForRet.setOnClickListener(v -> {
            selectAudio(REQ_AUDIO_FOR_MIX_FOR_RET);
        });
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) return;

        Uri audioUri = data.getData();
        if (audioUri == null) return;
        String audioPath = UriUtils.uri2File(audioUri).getAbsolutePath();
        if (requestCode == REQ_AUDIO_FOR_SP) {
            AeEntry.enterPitchSpeed(this, audioPath);
        } else if (requestCode == REQ_AUDIO_FOR_FMT_CONVERT) {
            AeEntry.enterFormatConvert(this, audioPath);
        } else if (requestCode == REQ_AUDIO_FOR_VOICE_CHANGE) {
            AeEntry.enterVoiceChange(this, audioPath);
        } else if (requestCode == REQ_AUDIO_FOR_CROP) {
            AeEntry.enterAudioCrop(this, audioPath);
        } else if (requestCode == REQ_AUDIO_FOR_CONCAT) {
            AeEntry.enterAudioConcat(this, audioPath);
        } else if (requestCode == REQ_AUDIO_FOR_MIX) {
            AeEntry.enterAudioMix(this, audioPath);
        } else if (requestCode == REQ_AUDIO_FOR_MIX_FOR_RET) {
            AeEntry.enterAudioMixForRet(this, audioPath, REQ_EDIT_AUDIO_FOR_MIX);
        }

        if (requestCode == REQ_EDIT_AUDIO_FOR_MIX) {
            mDataBinding.tvEditRet.setText("保存的音频的Uri为：" + audioUri.toString());
        }
    }

    private void selectAudio(int reqCode) {
        PermissionUtils.permission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .callback(new PermissionUtils.SingleCallback() {
                    @Override
                    public void callback(boolean isAllGranted, @NonNull List<String> granted, @NonNull List<String> deniedForever, @NonNull List<String> denied) {
                        if (isAllGranted) {
                            IntentUtil.pickAudio(AudioEditDemoActivity.this, reqCode);
                        } else {
                            ToastUtils.showShort(com.stark.common.res.R.string.permission_no_granted);
                        }
                    }
                })
                .request();
    }

}
